{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "b = np.array([1,1,1])\n",
    "print(a-b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.5 1.  1.5]\n",
      " [2.  2.5 3. ]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "b = np.array([2,2,2])\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  4  9]\n",
      " [16 25 36]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "print(np.pow(a, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 6 7]\n",
      " [6 7 8]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3]])\n",
    "b = np.array([[4],[5],[6]])\n",
    "print(a+b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-3]\n",
      " [-3]\n",
      " [-3]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1],[2],[3]])\n",
    "b = np.array([[4],[5],[6]])\n",
    "print(a-b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 5]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3],[4, 5, 6]])\n",
    "print(a[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 2. 3. 1.]\n",
      " [4. 5. 6. 1.]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3],[4, 5, 6]])\n",
    "b = np.hstack([a, np.ones((a.shape[0], 1))])\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3],[4, 5, 6]])\n",
    "b = np.argmax(a)\n",
    "print(b)\n",
    "c = np.array([3,1,2])\n",
    "d = np.argmax(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 5]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 2, 3, 4, 5, 6])\n",
    "b = np.array([-1, -1, 1, 0, 1, 0])\n",
    "print(a[b>0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 6]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "b = np.array([0, 2])\n",
    "print(a[np.arange(a.shape[0]), b])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True False]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(3)\n",
    "b = np.array([1, 1, 1])\n",
    "print(a==b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False False]\n",
      "[ True  True False]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([True, False, False])\n",
    "b = np.array([True, True, False])\n",
    "print(a & b)\n",
    "print(a | b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 1 1]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([True, False, False])\n",
    "b = np.array([1, 1, 1])\n",
    "print(a + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 4 10 18]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "b = np.array([4, 5, 6])\n",
    "print(np.multiply(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 1 2]\n",
      "[1 3]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "b = np.array([1, 0, 1])\n",
    "c = np.array([True, False, True])\n",
    "print(a[b])\n",
    "print(a[c])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 3]\n",
      " [5 6]]\n",
      "[2 6]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1, 2, 3], [4,5,6]])\n",
    "b =np.array([1,2])\n",
    "print(a[:, b])\n",
    "print(a[np.arange(2), b])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [4 5]\n",
      " [7 8]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1, 2],[3,4],[5,6]])\n",
    "b =np.array([[0],[1],[2]])\n",
    "print(a+b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1]\n",
      " [3 3]\n",
      " [5 5]]\n",
      "[[1 1]\n",
      " [3 3]\n",
      " [5 5]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.array([[1, 2],[3,4],[5,6]])\n",
    "a2 = np.array([[1, 2],[3,4],[5,6]])\n",
    "b =np.array([1,1])\n",
    "c =np.array([1,1,1])\n",
    "a1[:, b]-=c.reshape(-1, 1)\n",
    "a2[:, 1]-=c\n",
    "print(a1)\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4  8 12]\n",
      " [ 5 10 15]\n",
      " [ 6 12 18]]\n",
      "[[ 4  5  6]\n",
      " [ 8 10 12]\n",
      " [12 15 18]]\n",
      "[[ 4]\n",
      " [10]\n",
      " [18]]\n",
      "[[ 4 10 18]]\n"
     ]
    }
   ],
   "source": [
    "# 假设A和B是两个一维的NumPy数组\n",
    "A = np.array([1, 2, 3])\n",
    "B = np.array([4, 5, 6])\n",
    " \n",
    "# 使用广播机制来实现A乘以B的各个元素并拼成一个矩阵\n",
    "C = A[np.newaxis, :] * B[:, np.newaxis]\n",
    "D = A[:, np.newaxis] * B[np.newaxis, :]\n",
    "E = A[:, np.newaxis] * B[:, np.newaxis]\n",
    "F = A[np.newaxis,:] * B[np.newaxis, :]\n",
    "print(C)\n",
    "print(D)\n",
    "print(E)\n",
    "print(F)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 1 1]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 3, 1, 2])\n",
    "# 使用numpy.bincount计算元素出现次数\n",
    "b = np.bincount(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, 3, 1, 2])\n",
    "b = np.unique(a)\n",
    "print(b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
